﻿var App_TypeModel = $.extend(TradeModel, {
    postAction: 'add',
    url: '/sysmgr/type',
    postUrl: '',
    title: " ",
    idField: "typeId",
    titleField: "typeName",
    toolbar: 'App_Type_Toolbar',
    datagrid: 'App_Type_Grid'
});
function fun_App_TypeSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#App_Type_Grid').datagrid('reload');
    }

}

function fun_ReloadApp_TypeList() {
    $('#App_Type_Grid').treegrid('reload');
}



var App_Type_ItemModel = $.extend(TradeModel, {
	 selRow: null, 
	    editor:{
		    title: "属性",
			winid:'App_Type_Item_EditForm',
			formid:'App_Type_Item_EditForm_fm',
			width:720,
			height:300
		},  
	    postAction: 'add',
	    url: '/sysmgr/typeitem', 
	    title: "系统基础属性",
	    idField: "id",
	    titleField: "attrCode",
	    toolbar: 'App_Type_Item_Toolbar',
	    datagrid: 'App_Type_Item_Grid'  
}); 


App_Type_ItemModel.reload =function() {
	fun_ReloadApp_Type_ItemList();
    $('#App_Type_Item_Grid').datagrid('reload');
    $('#App_Type_Item_Grid').datagrid('clearSelections');
    $('#App_Type_Item_EditForm').dialog('close');
} 


function fun_FindRowById(id) {
    var cRows = $('#App_Type_Item_Grid').datagrid('getRows');
    if (id == undefined || id == '' || id == null) return null;
    for (i = 0; i < cRows.length; i++) {
        if (cRows[i][App_Type_ItemModel.idField] == id) {
            return cRows[i];
        }
    }
    return null;
}


function fun_ShowApp_Type_ItemEditForm(id) { 
    App_Type_ItemModel.selRow = fun_FindRowById(id);

    if (App_Type_ItemModel.selRow ==null) {
        App_Type_ItemModel.postAction = 'add';
        App_Type_ItemModel.selRow = {};

        var row = $('#App_Type_Grid').treegrid('getSelected');
        if(row){
    		App_Type_ItemModel.selRow.typeId = row.typeId;
    	}
    	else { 
    		App_Type_ItemModel.ShowMsg('提示','请选择类型！');
    		return;
    	}  
    }
    else {
        App_Type_ItemModel.postAction = 'update';
    } 
     

    $('#App_Type_Item_EditForm_fm').form('clear' );
    $('#App_Type_Item_EditForm_fm').form('load', App_Type_ItemModel.selRow);
    $('#App_Type_Item_EditForm').dialog({
        top: "50px",
        resizable: false,
        maximizable: false,
        width:700,
        height: 240
    }) 
    .dialog('open')
    .dialog('setTitle',   App_Type_ItemModel.selRow[App_Type_ItemModel.titleField]);
}
function fun_SaveApp_Type_Item() {
	App_Type_ItemModel.submit();
}
function fun_DelApp_Type_Item(title, id) {
	App_Type_ItemModel.del( id,title);
} 
 
function fun_App_Type_ItemSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#App_Type_Item_Grid').datagrid('reload');
    }

}
function fun_ReloadApp_Type_ItemList() {
    $('#App_Type_Item_Grid').datagrid('reload');
}
 
function navonclick(info) { 
	fun_ReloadApp_Type_ItemList();
}




$(function () {

    $('#App_Type_Grid').treegrid({
        fit: true,
        border: false,
        singleSelect: true,
        fitColumns: true,
        method: 'post',
        url: Onluy.basePath()+'/sysmgr/type/tree',
        idField: 'typeId',
        treeField: 'typeName',
        loader:App_TypeModel.Loader,
        loadFilter: App_TypeModel.loadfilter,
        toolbar: '#App_Type_Toolbar',
        columns:[[
            { field: 'typeName', title: '请选择类型', width: 300, formatter: function (value, row) { return value + '&nbsp;&nbsp;&nbsp;<span style="color:#cecece">(&nbsp;' + row['typeId'] + '&nbsp;)</span>'; } }
        ]],
        onLoadError: function (res) {
            $.messager.alert('错误', JSON.stringify(res), 'error');
        },
        onBeforeLoad: function (row, param) {
            param['roottypeid'] = $('#roottypeid').val();
            param['name'] = $('#App_Type_ss').searchbox('getName');
            param['value'] = $('#App_Type_ss').searchbox('getValue');
        },
        onSelect: function (rowData) {
            fun_ReloadApp_Type_ItemList(rowData);
        }
    });

    $('#attrCode').combobox({
		    url:Onluy.basePath()+'/sysmgr/attribute/simplelist',  
	    	method: 'get', 
		    data:{rows:1000}, 
		    valueField:'attrCode',
		    textField:'attrMemo'  ,
	        loader:App_Type_ItemModel.Loader, 
	        loadFilter: function(rows){
	        	var i=0;
	        	for(i=0;i< rows.length;i++){
	        		var temp= rows[i];
	        		 rows[i].attrMemo='-'+temp.attrCode +':'+temp.attrMemo;
	        	} 
	            return  rows;
	        },
	        filter: function (q, row) { 
	            return row["attrMemo"].toLowerCase().indexOf(q.toLowerCase()) >= 0;
	        },
	        onSelect: function(rec){    
	        	$('#attrMemo').val(rec.attrMemo);
	        	$('#attrValue').val(rec.attrValue);
	        },
		    required:true
		});
	  


    $('#App_Type_Item_Grid').datagrid({
        pageSize: 20,
        fit: true,
        singleSelect: true,
        checkbox: false,
        rownumbers: false,
        fitColumns: true,
        pagination: true, //分页控件
        method: 'get',
        idField: App_Type_ItemModel.idField,
        treeField: App_Type_ItemModel.titleField,
        url: Onluy.basePath()+App_Type_ItemModel.url+ '/list',
        loader:App_Type_ItemModel.Loader,
        toolbar: '#App_Type_Item_Toolbar', 
        columns: [
            [
                    { title: '属性编码', field: 'attrCode', width: 160, sortable: true },
                    { title: '属性说明', field: 'attrMemo', width: 200, sortable: true },
                    { title: '属性值', field: 'attrValue', width: 300, sortable: true },
					{
					    field: 'opt', title: '操作', width: 100, align: 'center', rowspan: 2,
					    formatter: function (value, rec) {
					        return '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/pencil.gif" onclick="fun_ShowApp_Type_ItemEditForm(\'' + rec[App_Type_ItemModel.idField] + '\');" title="修改' + rec[App_Type_ItemModel.titleField] + '" />'
                                + '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/delete.gif" onclick="fun_DelApp_Type_Item(\'属性编码' + rec[App_Type_ItemModel.titleField] + '\',\'' + rec[App_Type_ItemModel.idField] + '\');" title="删除' + rec[App_Type_ItemModel.titleField] + '" />';
					    }
					}
            ] 
        ],

        onSelect: function (rowIndex, rowData) {
        },
        onLoadError: function (res) {
            App_Type_ItemModel.ShowException("数据异常：" + JSON.stringify(res));
        },
        onLoadSuccess: function (data) {
            if(data.success){
            }
            else{
                 $.messager.alert('提示', data.msg );
            }
       },
        onBeforeLoad: function (param) {
        	var row = $('#App_Type_Grid').treegrid('getSelected');
        	if(row){ 
                param['typeId'] = row.typeId;
        	}
        	else { 
        		param['typeId'] = '-1';
        	}  
            param['name'] = $('#App_Type_Item_ss').searchbox('getName');
            param['value'] = $('#App_Type_Item_ss').searchbox('getValue');
            param['catchKey'] = Math.random();
        }
    });
    //设置分页控件  
    var p = $('#App_Type_Item_Grid').datagrid('getPager');
    $(p).pagination({
        pageSize: 20, //每页显示的记录条数，默认为10  
        pageList: [20, 30, 50, 100], //可以设置每页记录条数的列表  
        beforePageText: '第', //页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });

});


